clear all


use "$root\data\use.dta", replace



gen corn_insured=corn_insured_acres/(corn_planted_fsa+corn_failed_fsa)
gen soybeans_insured=soybeans_insured_acres/(soybeans_planted_fsa+soybeans_failed_fsa)  //divide by planned acres

replace corn_insured=1 if corn_insured>1&~missing(corn_insured)
replace soybeans_insured=1 if soybeans_insured>1&~missing(soybeans_insured)
replace corn_weighted_cover=corn_weighted_cover*corn_insured
replace soybeans_weighted_cover=soybeans_weighted_cover*soybeans_insured


xtset fips year
* drop *38c*


foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00
**# OLS

**## Planned planting acres
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_wet
	
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_cover

    reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_insured if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_insure
	

**## Planting ratio
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_wet
	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_weighted_cover  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_cover

	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p `crop'_insured  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_insure
	

**## Harvest ratio
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_wet

	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_cover
	
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_insured if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_insure


**## Yield
	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g   if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_w

	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover  if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_c
	
	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_insured if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_i
	

**## Production
	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_w
	

	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_weighted_cover if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_c
	
	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g `crop'_insured if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_i


restore
}


**## Combined plots
coefplot (corn_pplanted_wet, offset(-0.25)) /// 
	(corn_pplanted_cover, offset(0) msymbol(T)) ///
	(corn_pplanted_insure, offset(0.25) msymbol(S)), bylabel(Planned Acres, Corn)  || ///
	(soybeans_pplanted_wet, offset(-0.25)) /// 
	(soybeans_pplanted_cover, offset(0)  msymbol(T)) ///
	(soybeans_pplanted_insure, offset(0.25) msymbol(S)), bylabel(Planned Acres, Soybeans) || ///
	(corn_plratio_wet, offset(-0.25)) /// 
	(corn_plratio_cover, offset(0) msymbol(T)) ///
	(corn_plratio_insure, offset(0.25) msymbol(S)), bylabel(Planted Ratio, Corn) || ///
	(soybeans_plratio_wet, offset(-0.25)) /// 
	(soybeans_plratio_cover, offset(0) msymbol(T)) ///
	(soybeans_plratio_insure, offset(0.25) msymbol(S)), bylabel(Planted Ratio, Soybeans) || ///
	(corn_hratio_wet, offset(-0.25)) /// 
	(corn_hratio_cover, offset(0) msymbol(T)) ///
	(corn_hratio_insure, offset(0.25) msymbol(S)), bylabel(Harvest Ratio, Corn) || ///
	(soybeans_hratio_wet, offset(-0.25)) /// 
	(soybeans_hratio_cover, offset(0) msymbol(T)) ///
	(soybeans_hratio_insure, offset(0.25) msymbol(S)), bylabel(Harvest Ratio, Soybeans) || ///
	(corn_yield_w, label(Main Results) offset(-0.25)) /// 
	(corn_yield_c, offset(0) msymbol(T)) /// 
	(corn_yield_i, offset(0.25) msymbol(S)), bylabel(Yield, Corn) || ///
	(soybeans_yield_w, label(Main Results) offset(-0.25)) /// 
	(soybeans_yield_c, offset(0) msymbol(T)) /// 
	(soybeans_yield_i, offset(0.25) msymbol(S)), bylabel(Yield, Soybeans) || ///
	(corn_production_w, label(Main Results) offset(-0.25)) /// 
	(corn_production_c, offset(0) msymbol(T)) /// 
	(corn_production_i, offset(0.25) msymbol(S)), bylabel(Production, Corn) || ///
	(soybeans_production_w, label(Main Results) offset(-0.25)) /// 
	(soybeans_production_c, offset(0) msymbol(T)) /// 
	(soybeans_production_i, offset(0.25) msymbol(S)), bylabel(Production, Soybeans) ||, ///
	mcolor(black) mfcolor(white) msize(small) mlwidth(thin) ciopts(color(black)  lwidth(thin)) ///
	vertical keep(d*_b d*_p d*_g) yline(0, lcolor(gray) lwidth(0.2)) legend(rows(1) size(vsmall) region(lstyle(none)) colgap(20)) /// 
	byopts(yrescale compact cols(2) graphregion(fcolor(white) margin(l r+3 b-5 t-5)))  ///
	groups(d*_b = Pre-planting d*_p = Planting d*_g = Growing) ylabel(, labsize(vsmall) angle(45)) ///
	xlabel(1 "D0" 2 "D1" 3 "D2" 4 "D3" 6 "D0" 7 "D1" 8 "D2" 9 "D3" 11 "D0" 12 "D1" 13 "D2" 14 "D3", labsize(vsmall) angle(45)) ///
	plotlabels("Main" "Coverage" "% Insured") subtitle(, fcolor(white) bmargin(top) bcolor(white) size(small))
	graph export "$root/results_replication/appendix_figures/a6_insurance.png", as(png) name("Graph") width(3200) height(3600) replace

